package com.itheima.health.controller;


import com.itheima.health.resource.upload.UploadUtils;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
@RequestMapping("jersey")
public class JerseyController {




    //  MultipartFile   上传文件 封装到  MultipartFile
    //  uploadFile    <input type="file" name="uploadFile"><br>
    @RequestMapping("upload.do")
    public  String  upload(@RequestParam("uploadFile")MultipartFile file){
        try {
            String originalFilename = file.getOriginalFilename();
            String uuidFileName = UploadUtils.generateRandonFileName(originalFilename);
            //   上传 。。。本地磁盘  ----jersey  -----9001 服务器上
            Client client = new Client();//  获取jersey客户端对象
            WebResource resource = client.resource("http://127.0.0.1:9001/uploads/"+uuidFileName);
            resource.put(file.getBytes());//  put方法文件内容 传输远程 服务器上
            return "redirect:/upload/success.html";
        } catch (Exception e) {
            e.printStackTrace();
            return "redirect:/upload/error.html";
        }

    }
}
